#include <Arduino.h>
#include <lvgl.h>
#include <ui.h>
#include "cloud.hpp"

char ssid[63]="Xiaomi_BC0A";
char password[63]="75327532";


typedef enum {
    SSID_INPUT,
    SSIDPWD_INPUT,
    BBL_INPUT,
    BBLPWD_INPUT
} textInputStatus;

textInputStatus status = SSID_INPUT;

void btn_ssid_set(lv_event_t * e)
{
	// Your code here
	status = SSID_INPUT;
	lv_textarea_set_text(ui_TextArea2, ssid);
	Serial.printf("Setting ssid\n");
}

void btn_ssid_pwd_set(lv_event_t * e)
{
	status = SSIDPWD_INPUT;
	lv_textarea_set_text(ui_TextArea2, password);
	Serial.printf("Setting ssidpwd\n");
	// Your code here
}

void tast_connect_wifi(void* parameter){

}

void btn_connect_wifi(lv_event_t * e)
{
	// Your code here
	Serial.printf("ssid:\"%s\"\npassword:\"%s\"\n", ssid, password);
	// _ui_screen_change(&ui_Screen4, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_Screen4_screen_init);
	// xTaskCreate(tast_connect_wifi, "tast_connect_wifi", 1024,  NULL, 1, NULL);
	lv_label_set_text(ui_LabelConnectState, "Connecting to wifi...\n");
	connectToWifi(ssid, password);
	lv_label_set_text(ui_LabelConnectState, "Success !\n");
	_ui_screen_change(&ui_Screen3, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_Screen3_screen_init);
}

void btn_bbl_set(lv_event_t * e)
{
	// Your code here
	status = BBL_INPUT;
	lv_textarea_set_text(ui_TextArea2, "");
}

void btn_bbl_pwd_set(lv_event_t * e)
{
	// Your code here
	status = BBLPWD_INPUT;
	lv_textarea_set_text(ui_TextArea2, "");
}

void btn_connect_bbl(lv_event_t * e)
{
	// Your code here
}

void btn_func_input_ok(lv_event_t * e)
{
	// Your code here
	if (status == SSID_INPUT) {
		strncpy(ssid, lv_textarea_get_text(ui_TextArea2), sizeof(ssid) - 1);
	}
	if (status == SSIDPWD_INPUT) {
		strncpy(password, lv_textarea_get_text(ui_TextArea2), sizeof(ssid) - 1);
	}
	// if (status == BBL_INPUT) {
	// 	strncpy(ssid, lv_textarea_get_text(ui_TextArea2), sizeof(ssid) - 1);
	// }
	// if (status == BBLPWD_INPUT) {
	// 	strncpy(ssid, lv_textarea_get_text(ui_TextArea2), sizeof(ssid) - 1);
	// }
}